package com.view;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;
import com.bean.User;
import com.bluetooth.Dispatcher;
import com.firewarning.R;
import com.service.UserService;
import com.util.Message;
public class MainActivity extends BaseActivity {
    private ImageView toLogin;
    private ImageView toSetting;
    private Handler handler=new Handler();
    private RelativeLayout toRegister;
    private RelativeLayout callPolice;
    private RelativeLayout sendMessage;
    private RelativeLayout outFire;
    private VideoView playVedio;
    private BluetoothAdapter bluetoothAdapter; //本地蓝牙适配器
    private Dispatcher dispatcher;
    private String address=null;
    private int mode=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null) {
            actionBar.hide();
        }
        toLogin=(ImageView)findViewById(R.id.main_tologin);
        toSetting=(ImageView)findViewById(R.id.main_tosetting);
        callPolice=(RelativeLayout)findViewById(R.id.main_callpolice);
        outFire=(RelativeLayout)findViewById(R.id.main_rl_outfire);
        sendMessage=(RelativeLayout)findViewById(R.id.main_sendmessage);
        toRegister=(RelativeLayout)findViewById(R.id.main_toChose) ;
        toLogin.setOnClickListener(handler);
        toSetting.setOnClickListener(handler);
        toRegister.setOnClickListener(handler);
        callPolice.setOnClickListener(handler);
        outFire.setOnClickListener(handler);
        sendMessage.setOnClickListener(handler);
        playVedio=(VideoView)findViewById(R.id.paly_video);
         MediaController videoController=new MediaController(this);
        //自动播放res下的raw目录下的单个视频，循环播放
        String uri = "android.resource://" + getPackageName() + "/" + R.raw.video;

            playVedio.setVideoURI(Uri.parse(uri));
            playVedio.setMediaController(videoController);
            videoController.setMediaPlayer(playVedio);
            playVedio.requestFocus();
            playVedio.start();


    }

    class Handler implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.main_tologin:
                     MainActivity.this.startActivity(new Intent(MainActivity.this,LoginActivity.class));break;
                case R.id.main_tosetting:
                    MainActivity.this.startActivity(new Intent(MainActivity.this,SettingActivity.class));break;
                case R.id.main_toChose:
                    bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
                    if(bluetoothAdapter==null)
                    {
                        Toast.makeText(MainActivity.this,"设备不支持蓝牙",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        //蓝牙是否开启
                        if (!bluetoothAdapter.isEnabled()) {
                            bluetoothAdapter.enable();

                        }
                      MainActivity.this.startActivityForResult(new Intent(MainActivity.this,ChoseActivity.class),1);
                    }break;
                case R.id.main_callpolice:
                    if(!UserService.isLogin(MainActivity.this)){

                        Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
                    }
                    else if(address==null){
                        Toast.makeText(MainActivity.this,"请先连接蓝牙",Toast.LENGTH_SHORT).show();
                    }
                    else{
                        if(mode==2){
                            Intent intent = new Intent(MainActivity.this, ShowData.class);
                            Bundle bundle = new Bundle();// 创建Bundle对象
                            bundle.putString("address", address);
                            intent.putExtras(bundle);// 将Bundle对象放入到Intent上
                            if (address != null) {
                                MainActivity.this.startActivity(intent);
                            } else {
                                Toast.makeText(MainActivity.this, "请先连接蓝牙", Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            Toast.makeText(MainActivity.this,"请选择通知",Toast.LENGTH_SHORT).show();
                        }
                    }break;
                case R.id.main_rl_outfire:
                    if(!UserService.isLogin(MainActivity.this)){
                        Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
                    }  else if(address==null){
                        Toast.makeText(MainActivity.this,"请先连接蓝牙",Toast.LENGTH_SHORT).show();
                    }
                    else{

                    }break;
                case R.id.main_sendmessage:
                    if(!UserService.isLogin(MainActivity.this)){
                        Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
                    }  else if(address==null){
                        Toast.makeText(MainActivity.this,"请先连接蓝牙",Toast.LENGTH_SHORT).show();
                    }
                    else{
                         if(mode==1){
                            Intent intent = new Intent(MainActivity.this, WarningActivity.class);
                            Bundle bundle = new Bundle();// 创建Bundle对象
                            bundle.putString("address", address);
                            intent.putExtras(bundle);// 将Bundle对象放入到Intent上
                             MainActivity.this.startActivity(intent);
                        }else{
                             Toast.makeText(MainActivity.this,"请选择位置显示",Toast.LENGTH_SHORT).show();
                         }

                    }break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1&&data!=null){
            address=data.getStringExtra("address");
            String name=data.getStringExtra("name");
            mode=data.getIntExtra("mode",0);
            Toast.makeText(MainActivity.this,"已连接"+name,Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(MainActivity.this,"没有选择要连接的蓝牙设备",Toast.LENGTH_LONG).show();
        }
    }
    public void onDestroy() {
        super.onDestroy();
        if(dispatcher!=null)
        {
            dispatcher.destory();
        }
    }
}
